package org.lxg.springmvc.controller;

import org.lxg.common.pojo.User;
import org.lxg.springmvc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class SpringmvcController {

    @Autowired
    public UserService userService;

    @RequestMapping(value = "/hello")
    public ModelAndView say() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("hello", "世界");
        // 寻找web-inf目录下的jsp/hello.jsp文件
        mav.setViewName("hello");
        return mav;
    }

    @RequestMapping(value = "/insertUser")
    public String inserUser() {
        int i = userService.insertUser("1", "C");
        if (i == 0) {
            return "sucess";
        } else {
            return "fail";
        }

    }

    @RequestMapping(value = "/findUsers")
    @ResponseBody
    public List<User> findUsers() {
        List<User> list = userService.findUsers();
        return list;
    }

    /**
     * Spring 整合 Springmvc
     *  1 Spring 负责扫描注入类：数据源等
     *  2 Springmvc 负责扫描controller
     */
}